﻿using System;
using System.Globalization;

namespace Loki.UI
{
    public class SubViewModel<TParent, TModel> : ViewModel<TModel>, ISubViewModel
        where TParent : ViewModel<TModel>
    {
        IViewModel ISubViewModel.Parent
        {
            get { return this.Parent; }
            set { this.Parent = (TParent)value; }
        }

        public override string FullDisplayName
        {
            get
            {
                return string.Format(CultureInfo.InvariantCulture, Messages.VMMessages.SUBVM_FULLNAME, Parent.FullDisplayName, DisplayName);
            }
        }

        private TParent _Parent;

        public TParent Parent
        {
            get
            {
                return _Parent;
            }

            set
            {
                if (_Parent != value)
                {
                    if (_Parent != null)
                    {
                        OnParentChanging(EventArgs.Empty);
                    }

                    _Parent = value;
                    if (value != null)
                    {
                        OnParentChanged(EventArgs.Empty);
                    }
                }
            }
        }

        #region Events

        #region ParentChanging
        public event EventHandler ParentChanging;

        protected virtual void OnParentChanging(EventArgs e)
        {
            EventHandler L_Handler = ParentChanging;

            if (L_Handler != null)
            {
                L_Handler(this, e);
            }
        }

        #endregion

        #region ParentChanged
        public event EventHandler ParentChanged;

        protected virtual void OnParentChanged(EventArgs e)
        {
            EventHandler L_Handler = ParentChanged;

            if (L_Handler != null)
            {
                L_Handler(this, e);
            }
        }

        #endregion

        #endregion
    }
}
